﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using YouMa.Model;
using YouMa.Reposity;

namespace YouMa.Controllers
{
    [Produces("application/json")]
    [Route("api/User")]
    public class UserController : BaseController
    {
        private readonly UserReposity UserReposity;
        public UserController(UserReposity _UserReposity)
        {

            UserReposity = _UserReposity;
        }

        #region
        /// <summary>
        /// 获取用户个人信息列表
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        [HttpGet, Route("GetUserInfoList")]
        public IActionResult GetUserInfoList(string phone)
        {
            var list = UserReposity.GetList<User>(a => a.Phone == phone);
            if (list != null && list.Count > 0)
            {
                return Ok(new { msg = "获取列表成功", isSuccess = true });
            }
            else
            {
                return Ok(new { msg = "获取列表失败", isSuccess = false });
            }
        }

        /// <summary>
        /// 获取单个用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet, Route("GetUserById")]
        public IActionResult GetUserById(int id)
        {
            var model = UserReposity.GetEntity<User>(a => a.Id == id);
            if (model != null)
            {
                return Ok(new { msg = "获取成功", isSuccess = true });
            }
            else
            {
                return Ok(new { msg = "该用户Id不存在", isSuccess = false });
            }

        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost, Route("Add")]
        public IActionResult Add(User entity = null)
        {
            if (entity == null)
            {
                return Ok(new { msg = "参数为空", isSuccess = false });
            }
            else
            {
                var model = UserReposity.InsertEntity<User>(entity);
                if (model)
                {
                    return Ok(new { msg = "添加成功", isSuccess = true });
                }
                else
                {
                    return Ok(new { msg = "添加失败", isSuccess = false });
                }
            }
        }

        /// <summary>
        /// 编辑用户
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost, Route("Update")]
        public IActionResult Update(User entity = null)
        {
            if (entity == null)
            {
                return Ok(new { msg = "参数为空", isSuccess = false });
            }
            else
            {
                var model = UserReposity.GetEntity<User>(a => a.Id == entity.Id);
                model.UserName = entity.UserName;
                model.Phone = entity.Phone;
                model.Brithday = entity.Brithday;
                model.Remark = entity.Remark;
                model.Address = entity.Address;
                model.Age = entity.Age;
                model.Sex = entity.Sex;
                model.Photo = entity.Photo;
                model.College = entity.College;
                model.Job = entity.Job;
                var exit = UserReposity.UpdateEntity<User>(entity);
                if (exit)
                {
                    return Ok(new { msg = "编辑成功", isSuccess = true });
                }
                else
                {
                    return Ok(new { msg = "编辑失败", isSuccess = false });
                }
            }
        }

        /// <summary>
        /// 删除用户,批量删除
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        [HttpPost, Route("Dels")]
        public IActionResult Dels(List<User> list = null)
        {
            if (list.Count == 0)
            {
                return Ok(new { msg = "参数为空", isSuccess = false });
            }

            bool exit = UserReposity.DeleteEntityRange<User>(list);
            if (exit)
            {
                return Ok(new { msg = "删除操作成功", isSuccess = true });
            }
            else
            {
                return Ok(new { msg = "删除操作失败", isSuccess = false });
            }
        }
        #endregion
    }
}